package com.erlang.spirngbootmybatis.mapper;

import com.erlang.spirngbootmybatis.entity.Customer;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 顾客Mapper接口
 *
 * @author ZGQ
 */
public interface CustomerMapper {

    //推荐使用#{}取值，不要用${},因为存在注入的风险
    @Insert("INSERT INTO customer(name,phone,create_time,age) VALUES(#{name}, #{phone}, #{createTime},#{age})")
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")   //keyProperty java对象的属性；keyColumn表示数据库的字段
    int insert(Customer customer);

    //column指数据库中的列，property是指实体的属性名，如果一致就不需要写
    @Select("SELECT * FROM customer")
    @Results({
            @Result(column = "create_time",property = "createTime")
    })
    List<Customer> getAll();

    @Select("SELECT * FROM customer WHERE id = #{id}")
    @Results({
            @Result(column = "create_time",property = "createTime")
    })
    Customer findById(Long id);


    @Update("UPDATE customer SET name=#{name} WHERE id =#{id}")
    void update(Customer customer);

    @Delete("DELETE FROM customer WHERE id =#{customerId}")
    void delete(Long customerId);
}
